package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.baoyz.widget.PullRefreshLayout;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestV2Listener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.bean.base.NormalResponseB;
import com.cn.aam.checaiduo.bean.me.NoticeB;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.me.adapter.NoticeAdapter;
import com.cn.aam.checaiduo.widget.MultiStateView;

import java.util.List;

import butterknife.BindView;

/**
 * monker on 2017/6/1.
 */

public class MessageListActivity extends BaseActivity {


    @BindView(R.id.id_recyclerview)
    RecyclerView idRecyclerview;
    @BindView(R.id.id_multistateview)
    MultiStateView idMultistateview;
    @BindView(R.id.id_swipe)
    PullRefreshLayout idSwipe;
    private NoticeAdapter adapter;

    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, MessageListActivity.class);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void initView() {
        ToolBarManager.with(this, WMContent).setTitle("消息")
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setTitleColor(R.color.textColor)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back);

        idSwipe.setEnabled(false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        adapter = new NoticeAdapter(mContext);
        idRecyclerview.setLayoutManager(layoutManager);
        idRecyclerview.setAdapter(adapter);
        getNotify();
        adapter.setOnItemClickListener(new BaseRecycerAdapter.OnItemClick() {
            @Override
            public void onClick(int position) {
                startActivity(MessageDetailActivity.newInstance(mContext, adapter.getItem(position), "系统公告"));
            }
        });
    }

    /**
     * 时间：2017/6/15
     * 描述：系统公告
     */
    private void getNotify() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .getNotify(AppData.getToken())
                .compose(RxSchedulers.<NormalResponseB<List<NoticeB>>>toMain())
                .subscribe(new OnRequestV2Listener<List<NoticeB>>(this) {
                    @Override
                    protected boolean isShowProgressBar() {
                        return false;
                    }

                    @Override
                    public void onNext(NormalResponseB<List<NoticeB>> value) {
                        super.onNext(value);
                        if (value.getIsLogged() != Const.STATE_LOGIN) {
                            goLogin();
                            return;
                        }
                        idMultistateview.setViewState((value == null || value.getData().size() == 0)
                                ? MultiStateView.VIEW_STATE_EMPTY : MultiStateView.VIEW_STATE_CONTENT);
                        adapter.setDataToAdapter(value.getData());
                    }
                });
    }

    @Override
    public void onBackPressed() {
        if (AppData.getActivityCount() == 1) {//通知栏跳转而来
            UIRouter.getInstance().openPage(mContext,UIRouter.HOME.AC_MAIN);
//            startActivity(MainActivity.newInstance(mContext));
        } else {
            super.onBackPressed();
        }
    }
}
